<template>
	<view class="real-weap">
		<view class="login-head u-text-center">
			<image src="https://clhy-img.oss-cn-beijing.aliyuncs.com/ownerImage/logo.svg" style="width: 136rpx; height: 136rpx;"></image>
			<view class="u-font-32 u-m-t-40" style="color: #333;">欢迎来到我爱我屋 </view>
		</view>
		<view class="u-p-l-40 u-p-r-40 u-m-t-40 u-m-b-50">
			<u-form :model="form" ref="rules">
				<u-form-item label="姓名" prop="name">
					<u-input v-model="form.name" placeholder="请输入姓名" />
				</u-form-item>
				<u-form-item label-width='144' label="身份证号" prop="id">
					<u-input placeholder="请输入身份证号" v-model="form.id" />
				</u-form-item>
			</u-form>
		</view>
		<view class="u-m-t-80 u-p-l-50 u-p-r-50">
			<u-button :ripple="true" @click="confirm" type="warning" ripple-bg-color="#FDD361">确认</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: '',
				type: 'text',
				border: false,
				form: {
					name: null,
					id: '',
				},
				rules: {
					name: [{
							required: true,
							message: '请输入的姓名',
							// 可以单个或者同时写两个触发验证方式
							trigger: 'change,blur'
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// this.$u.test.mobile()就是返回true或者false的
								if (this.$u.test.rangeLength(value, [2, 10])) {
									return this.$u.test.chinese(value);
								} else {
									return false
								}
							},
							message: '请输入正确的姓名',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					],

					id: [{
							required: true,
							message: '请输入身份证号码',
							trigger: 'change,blur',
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								return this.$u.test.idCard(value);
							},
							message: '身份证号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					]
				},
				show: false,
				content: '',
				changPhone: null,
			}
		},
		onReady() {
			this.$refs.rules.setRules(this.rules);
		},
		methods: {
			confirm() {
				let _this = this;

				this.$refs.rules.validate(valid => {
					if (valid) {
						console.log(_this.form,uni.getStorageSync('phone'))
						// 调取后台身份证接口
						_this.$request.api.name({
							name: _this.form.name,
							id_card: _this.form.id,
							phone: uni.getStorageSync('phone'),
							open_id: uni.getStorageSync('openId'),
						}).then(res => {
							// 1.没手机号
							// 2.两手几号不一样
							// 3.新用户
							console.log(res)
							if (res.data.data.data.sign==1) {
								uni.setStorageSync('loginFlag', true),
								uni.setStorageSync('changePhone', false),
								_this.$Router.push({
									path: '/my/address/address'
								})
							} else if (res.data.data.data == 'identityCard') {
								uni.showModal({
									content: '您的姓名和身份证不符，请重新输入',
									showCancel: false,
									confirmText: '确认',
									confirmColor: '#F9A832',
									success: function(res) {
										if (res.confirm) {
											console.log('用户点击确定');
										}
									}
								});
							} else if (res.data.data.data.sign == 2) {
								_this.changPhone = res.data.data.data.phone;
								uni.setStorageSync('changePhone', true),
									uni.setStorageSync('name', _this.form.name),
									uni.setStorageSync('id', _this.form.id),
									uni.showModal({
										content: '当前手机号与助业账户不符\n请切换为: ' + _this.changPhone,
										showCancel: false,
										confirmText: '确认',
										confirmColor: '#F9A832',
										success: function(res) {
											if (res.confirm) {
												_this.$Router.push({
													path: '/homePage1/phone/phone'
												})
											}
										}
									});
							}

						})


					} else {
						console.log('验证失败');
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.real-weap {
		width: 100vw;
		height: 100vh;

		.login-head {
			height: 200rpx;
		}

		.u-form-item--left {
			display: none;
		}
	}
</style>
